package io.gitee.yth.design.pattern.test.core.pattern.behavioral.mediator;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author yth
 * @since 2023-05-01-16:40
 */
public class FormDirector implements ActionListener {

    private Button ok;
    private Button all;
    private CheckBox music;
    private CheckBox reading;
    private CheckBox programing;

    private final ArrayList<String> hobbies = new ArrayList<>();

    public void createWidgets() {
        ok = new Button(this);
        all = new Button(this);
        music = new CheckBox(this);
        reading = new CheckBox(this);
        programing = new CheckBox(this);
    }

    @Override
    public void onActionPerformed(Widget w, Object... args) {
        if (ok.equals(w)) {
            System.out.println("ok. your hobbies are " + hobbies);
        } else if (all.equals(w)) {
            if (hobbies.size() != 3) {
                music.setChecked(true);
                reading.setChecked(true);
                programing.setChecked(true);
                hobbies.clear();
                hobbies.addAll(Arrays.asList("music", "reading", "programing"));
                System.out.println("select all ...");
            } else {
                music.setChecked(false);
                reading.setChecked(false);
                programing.setChecked(false);
                hobbies.clear();
                System.out.println("cancel all ...");
            }
        } else if (music.equals(w)) {
            if ((boolean) args[0]) {
                System.out.println("click music ...");
                hobbies.add("music");
            } else {
                System.out.println("cancel music ...");
                hobbies.remove("music");
            }
        } else if (reading.equals(w)) {
            if ((boolean) args[0]) {
                System.out.println("click reading ...");
                hobbies.add("reading");
            } else {
                System.out.println("cancel reading ...");
                hobbies.remove("reading");
            }
        } else if (programing.equals(w)) {
            if ((boolean) args[0]) {
                System.out.println("click programing ...");
                hobbies.add("programing");
            } else {
                System.out.println("cancel programing ...");
                hobbies.remove("programing");
            }
        } else {
            System.out.println("unknown widget: " + w);
        }
    }


    /* ----- 提供getter，用于测试 ----- */

    public Button getOk() {
        return ok;
    }

    public Button getAll() {
        return all;
    }

    public CheckBox getMusic() {
        return music;
    }

    public CheckBox getReading() {
        return reading;
    }

    public CheckBox getPrograming() {
        return programing;
    }
}
